.TH std::vector::swap 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::vector::swap \- std::vector::swap

.SH Synopsis
   Defined in header <vector>
   static void swap( reference x, reference y );            \fI(until C++20)\fP
   constexpr static void swap( reference x, reference y );  \fI(since C++20)\fP

   Swaps the contents of x and y as if by bool b = x; x = y; y = b;.

.SH Parameters

   x - std::vector<bool>::reference value to swap with y
   y - std::vector<bool>::reference value to swap with x

.SH Return value

   \fI(none)\fP

.SH Complexity

   Constant.

.SH Example


// Run this code

 #include <iostream>
 #include <vector>

 void println(std::string_view fmt, std::vector<bool> const& vb = {})
 {
     for (std::cout << fmt; bool const e : vb)
         std::cout << e << ' ';
     std::cout << '\\n';
 }

 int main()
 {
     println("swap elements of the same vector:");
     std::vector<bool> x{1, 0};
     println("before swap, x: ", x);
     x.swap(x[0], x[1]); // same as std::vector<bool>::swap(x[0], x[1]);
     println("after swap,  x: ", x);

     println("swap elements of two different vectors:");
     std::vector<bool> y{0, 0, 1};
     println("before swap, x: ", x);
     println("before swap, y: ", y);
     y.swap(x[0], y[2]); // same as std::vector<bool>::swap(x[0], y[2]);
     println("after swap,  x: ", x);
     println("after swap,  y: ", y);
 }

.SH Output:

 swap elements of the same vector:
 before swap, x: 1 0
 after swap,  x: 0 1
 swap elements of two different vectors:
 before swap, x: 0 1
 before swap, y: 0 0 1
 after swap,  x: 1 1
 after swap,  y: 0 0 0

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

     DR    Applied to              Behavior as published               Correct behavior
   LWG 814 C++98      the description of this member function was      added
                      missing

.SH See also

   reference              proxy class representing a reference to a single bool
                          \fI(class)\fP
   swap                   swaps the contents
                          \fI(public member function of std::vector<T,Allocator>)\fP
   std::swap(std::vector) specializes the std::swap algorithm
                          \fI(function template)\fP
